package controllers
{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.FileReference;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.utils.ByteArray;

import mx.rpc.AsyncToken;
import mx.rpc.Responder;
import mx.rpc.http.HTTPService;
import mx.rpc.remoting.RemoteObject;

import org.flexspeedup.utils.SwizDispatcherBase;

import types.BlobstoreServiceTypes;

/**
 * A test implementation of the remote blobstore services
 */
public class BlobstoreController extends SwizDispatcherBase
{

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new <code>BlobstoreContoller</code> instance.
     */
    public function BlobstoreController()
    {
        super();
    }

    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------
    
    //----------------------------------
    //  file
    //----------------------------------
    /**
     *  @private
     */
    private var file:FileReference;

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------
    //----------------------------------
    // blobstoreService
    //----------------------------------
    [Inject(source="blobstoreService")]
    /**
     * blobstoreService bean
     */
    public var blobstoreService:RemoteObject;

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------
    
    public function testEcho(message:String):void
    {
        var token:AsyncToken = blobstoreService.getOperation(
            BlobstoreServiceTypes.ECHO).send(message);
        token.addResponder(new Responder(
            echo_resultHandler, 
            faultHandler
        ));
    }


    public function uploadImage(file:FileReference):void
    {
        this.file = file;
        var token:AsyncToken = 
            blobstoreService.getOperation(BlobstoreServiceTypes.GET_UPLOAD_URL).send();  
        token.addResponder(new Responder(
            getUploadURL_resultHandler,
            faultHandler
        ));
    }

    //--------------------------------------------------------------------------
    //
    //  Event Handlers
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  faultHandler
    //----------------------------------
    /**
     * @private
     */
    private function faultHandler(info:Object):void
    {
        trace (info.message);        
    }
    
    //----------------------------------
    //  echo_resultHandler
    //----------------------------------
    /**
     * @private
     */
    private function echo_resultHandler(data:Object):void
    {
        trace (data.result);
    }   

    //----------------------------------
    //  getUploadURL_resultHandler
    //----------------------------------
    /**
     * @private
     */
    private function getUploadURL_resultHandler(data:Object):void
    {
        var request:URLRequest = new URLRequest(data.result);
        request.method = URLRequestMethod.POST;
        file.upload(request, "file");
        file.addEventListener(IOErrorEvent.IO_ERROR, file_ioErrorHandler);
        file.addEventListener(Event.COMPLETE, file_completeHandler);
    } 

    //----------------------------------
    //  file_ioErrorHandler
    //----------------------------------
    /**
     * @private
     */
    private function file_ioErrorHandler(event:IOErrorEvent):void
    {   
        trace (event.text);
    }

    //----------------------------------
    //  file_completeHandler
    //----------------------------------
    /**
     * @private
     */
    private function file_completeHandler(event:Event):void
    {
        trace ("upload complete!");
    }
    
}
}   